import axios from 'axios'
import {Indicator, Toast} from 'mint-ui';
import store from '../store'
import router from '../router'

// axios 配置
const axiosInstance = axios.create({
    baseURL: '/interest',
    timeout: 10000,
    headers:{
        // "Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
        "Content-Type":"application/json;charset=UTF-8"
    }
})

// http request 拦截器
axiosInstance.interceptors.request.use(
    config => {
        if (config.showLoading) Indicator.open({text: '加载中...',spinnerType: 'fading-circle'});
        if (!!store.state.users.currentUser.UserToken) config.headers.Authorization = 'bearer ' + store.state.users.currentUser.UserToken;
        return config;
    },
    err => {
        // Indicator.close();
        return Promise.reject(err);
    });

// http response 拦截器
axiosInstance.interceptors.response.use(
    response => {
        Indicator.close();
        return response;
    },
    error => {
        Indicator.close();
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    // 401 清除token信息并跳转到登录页面
                    Toast({
                        message: '未授权或登录已失效，请重新登录！',
                        duration: 2000,
                        // iconClass: 'iconfont jgsc-fail'
                    });
                    store.commit('users/clearUser');
                    router.replace({
                        path: '/login',
                        query: {redirect: router.currentRoute.fullPath}
                    });
                    break;
                case 403:
                    // 403 无权限，跳转到首页
                    Toast({
                        message: '无权限访问该页！',
                        duration: 2000,
                        iconClass: 'iconfont jgsc-fail'
                    });
                    router.replace({
                        path: '/',
                        query: {redirect: router.currentRoute.fullPath}
                    });
                    break;
            }
        }
        return Promise.reject(error);
    });


export default {
    /*
    * get请求
    * @param url
    * @param api
    * @returns {Promise}
    */
    get(url,params={},set={showLoading: true}){
        return new Promise((resolve,reject)=>{
            axiosInstance.get(url,{
                params,
                showLoading: set.showLoading,
            })
                .then(response => {
                    resolve(response)
                })
                .catch(error => {
                    reject(error)
                })
        })
    },

    /*
    * post请求
    * @param url
    * @param api
    * @returns {Promise}
    */
    post(url,data={},config={showLoading: true,file: false}){
        return new Promise((resolve,reject) => {
            if(config.file) config.headers = {'Content-Type':'multipart/form-data'};
            axiosInstance.post(url,data,config)
                .then(response => {
                    resolve(response)
                })
                .catch(error => {
                    reject(error)
                })
        })
    },
    put(url,data,config){
        return new Promise((resolve,reject)=>{
            config.headers = {'Content-Type':'multipart/form-data'};
            axiosInstance.put(url,data,config)
                .then(response=>{
                    resolve(response);
                })
                .catch(error=>{
                    reject(error);
                })
        })
    }
}

 
  


